uniform	sampler2D bearTex;
uniform int shadowToggle;

varying vec4 lightFactor;
varying vec4 TexCoords0;
varying vec4 TexCoords1;

uniform sampler2D ShadowMap;
varying vec4 ShadowCoord;

void main()
{
	gl_FragColor=texture2D(bearTex, TexCoords1.st);

	   // Uncomment to do per fragment lighting
        //        vec3 L = normalize(gl_LightSource[0].position.xyz - vertexPos);   
        //        vec4 Idiff = gl_FrontLightProduct[0].diffuse * max(dot(normalVector,L), 0.0);  
        //        Idiff = clamp(Idiff, 0.0, 1.0); 

        //        gl_FragColor += Idiff;

        gl_FragColor += lightFactor;
    
    if (shadowToggle == 1) {
        //check the shadow map
        vec4 shadowCoordinateWdivide = ShadowCoord / ShadowCoord.w ;
        
        // Used to lower moiré pattern and self-shadowing
        shadowCoordinateWdivide.z += 0.0005;
        
        float distanceFromLight = texture2D(ShadowMap,shadowCoordinateWdivide.st).z;
        
        float shadow = 1.0;
        if (ShadowCoord.w > 0.0)
            shadow = distanceFromLight < shadowCoordinateWdivide.z ? 0.5 : 1.0 ;
        
        gl_FragColor *=	 shadow;
    }
 
// gl_FragColor=vec4(1,0,0,0);	   
}
